<?php
$isReloadable = false;
require_once("../../../pers/inc.config.php");
require_once('../../_inc/inc.session.php');
require_once('../../_inc/funct.html.php');

$sql_sel = "SELECT m.cod_modulo, m.descr as m_descr "
		.", p.id_modulo, p.cod_pagina, p.descr AS p_descr "
		.", c.* "
		.", ra.cod_regola AS reg_acm "
		.", rt.cod_regola AS reg_tra "
		." FROM wfm_comandi c "
		." LEFT JOIN wfm_pagine p ON p.id_pagina = c.id_pagina " 
		." LEFT JOIN wfm_moduli m ON p.id_modulo = m.id_modulo "
		." LEFT JOIN wfm_regole ra ON ra.id_regola = c.id_regola_acm "
		." LEFT JOIN wfm_regole rt ON rt.id_regola = c.id_regola_tra "
		." WHERE c.id_comando = ".reqVarInt('id');
$objDB->query($sql_sel,'selRec');
if(!$aRow = $objDB->fetch('selRec')) {
	backOnErr("id. errato");
}

$aVarExt['HtmlPopUpRegAcm'] = "<a href=\"javascript:apriPopUpInsReg('id_regola_acm','acm');\">"
	."<img src=\"".OVF_URL_ROOT."/app/_img/popUp.png\" title=\"aggiungi una nuova regola\" border=\"0\"></a>";
$aVarExt['HtmlPopUpRegTra'] = "<a href=\"javascript:apriPopUpInsReg('id_regola_tra','tra');\">"
	."<img src=\"".OVF_URL_ROOT."/app/_img/popUp.png\" title=\"aggiungi una nuova regola\" border=\"0\"></a>";
$aVarExt['HtmlPopUpPagFin'] = "<a href=\"javascript:apriPopUpInsPag('id_pagina_fin');\">"
	."<img src=\"".OVF_URL_ROOT."/app/_img/popUp.png\" title=\"aggiungi una nuova pagina\" border=\"0\"></a>";
		
reqDef('cod_comando',$aRow['cod_comando']);
reqDef('descr',$aRow['descr']);
reqDef('id_regola_acm',$aRow['id_regola_acm']);
reqDef('stato_fin',$aRow['stato_fin']);
reqDef('id_pagina_fin',$aRow['id_pagina_fin']);
reqDef('id_modulo_tra',$aRow['id_modulo_tra']);
reqDef('stato_tra',$aRow['stato_tra']);
reqDef('id_pagina_tra',$aRow['id_pagina_tra']);
reqDef('id_regola_tra',$aRow['id_regola_tra']);
reqDef('nota',$aRow['nota']);
reqDef('idm',$aRow['id_modulo']);
reqDef('idp',$aRow['id_pagina']);
	
$aCamForm = array();
require("_inc/const.campiCmd.php");

if (reqVar('azione') == 'salva') {
	$msgPag = conferma($aCamForm);
} 


$sCmdR = "";
$sCmdR .= "<a class=\"cmd1\" href=\"javascript:submAz('salva');\" >salva</a>";
$sCmdR .= cmdTorna('torna');

$sHtmlMod = "<table class=\"tab1\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\" width=\"100%\" align='center'>\n";
$sHtmlMod .= getHtmlRigaRif();
$sHtmlMod .= "<tr>\n";
$sHtmlMod .= "<td class='lab1' colspan='3'>Modulo</td>\n";
$sHtmlMod .= "<td class='txt1' colspan='3'>".$aRow['cod_modulo']."</td>\n";
$sHtmlMod .= "<td class='txt1' colspan='14'>".$aRow['m_descr']."</td>\n";
$sHtmlMod .= "</tr>\n";
$sHtmlMod .= "<tr>\n";
$sHtmlMod .= "<td class='lab1' colspan='3'>Pagina</td>\n";
$sHtmlMod .= "<td class='txt1' colspan='3'>".$aRow['cod_pagina']."</td>\n";
$sHtmlMod .= "<td class='txt1' colspan='14'>".$aRow['p_descr']."</td>\n";
$sHtmlMod .= "</tr>\n";
$sHtmlMod .= "</table>";

$aOpz['sottotit'] = $sHtmlMod;

testaHtml('Gestione work-flow','Modifica comando pagina',$msgPag,'gest',$aOpz);
?>
<script language="JavaScript" type="text/javascript">
function submAz(az) {
	document.frm1.azione.value = az;
	document.frm1.submit();
}
function apriPopUpInsReg(campo,tr){
  	url = "regInsPopUp.php?id=<?=reqvar('idm');?>&tr="+tr+"&campo="+campo+"&xsn=<?=XSN?>";
	window.open(url, 'nuovaPag','width=730,height=300,top=50,left=50,resizable=yes,scrollbars=yes');
}
function apriPopUpInsPag(campo){
  	url = "pagInsPopUp.php?id=<?=reqvar('idm');?>&xsn=<?=XSN?>&campo=" + campo;
	window.open(url, 'nuovaPag','width=730,height=300,top=50,left=50,resizable=yes,scrollbars=yes');
}
</script>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="right">
			<? echo $sCmdR; ?>
      </td>
   </tr>
</table>
<?=getHtmlForm('frm1','',array('enctype'=>true));?>
<?=getHtmlHidden('azione','...');?>
<?=getHtmlHidden('id');?>
<?=getHtmlHidden('cod_modulo_old');?>
<?=getHtmlHidden('xsn',XSN);?>
<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
	<?=getHtmlRigaRif();?>
   <tr>
      <td class="tit1" colspan='20' align='center'>Comando pagina</td>
   </tr>
<?
echo getHtmlCampiForm($aCamForm,'inp',$aVarExt);
?>
</table>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="right">
			<? echo $sCmdR; ?>
      </td>
   </tr>
</table>
<?
echo "</form>\n";
codaHtml();
exit();
function conferma($aCamForm) {
	global $objDB;
	$msg = '';
	$_REQUEST['cod_comando'] = strtolower(trim(reqVar('cod_comando')));
	if (strlen(reqVar('cod_comando'))< 4) {
		$msg .= "Codice obbligatorio di almeno 4 caratteri<br>";
	}
	if (preg_match("/[^_a-z0-9]/",reqVar('cod_comando'))) {
		$msg = "Codice '".reqVar('cod_comando')."' puo' contenere solo lettere numeri e _<br>";
	}
	if (trim(reqVar('descr'))=='') {
		$msg .= "Descrizione obbligatoria<br>";
	}
	
	$sqlSel = "SELECT cod_comando FROM wfm_comandi  " 
			." WHERE cod_comando = '".reqVarAS('cod_comando')."'"
			." AND id_pagina = ".reqVarInt('idp')
			." AND id_comando <> ".reqVarInt('id');
	$objDB->exec($sqlSel,'chkDup');
	$aRow = $objDB->fetch('chkDup');
	if ($aRow) {
		$msg .= "Codice gia' esistente<br>";
	}
	if ($msg != '') {
		return $msg;
	}
	$sSql = "UPDATE wfm_comandi SET ";
	foreach($aCamForm as $rX => $aRX) {
		foreach($aRX as $jX => $aCX) {
			switch ($aCX[4]) {
				case 'date':
					$sSql .= $aCX[0]." = '".reqVarStr2YMD($aCX[0])."', ";
					break;
				default:
					$sSql .= $aCX[0]." = '".reqVarAS($aCX[0])."', ";
					break;
			}			
		}
	}
	$sSql = substr($sSql,0,-2)." WHERE id_comando = ".reqVarInt('id');
	$objDB->exec($sSql,'updRec');
	header("Location: ".urlTorna(_t("comando modificato")));
	exit();
}
